package com.example.demo.threadTest;

import java.util.concurrent.*;

/**
 * @author leiliang
 * @version 1.0
 * @Description
 * @date 2022/8/12 15:30
 */
public class testTread {


    public static void main(String[] args) {

        System.out.println("开始main线程..." + Thread.currentThread().getName());
        //jav8写法：
        Runnable runnable = () -> System.out.println("执行runnable任务1..." + Thread.currentThread().getName());

        //jav8写法：泛型代表了任务执行的返回类型
        Callable<String> callable = () -> {
            System.out.println("执行callable计算任务..." + Thread.currentThread().getName());
            return "返回callable任务结果";
        };
        ExecutorService service = Executors.newFixedThreadPool(2);

        //Runnable任务使用execute方法
        service.execute(runnable);
        System.out.println("开始main线程2..." + Thread.currentThread().getName());
        //Callable任务使用submit方法。这个方法是异步的。。
        Future future = service.submit(callable);

        System.out.println("结束main线程..." + Thread.currentThread().getName());
        try {
            System.out.println(future.get());
        } catch (InterruptedException | ExecutionException e) {

        }

//        //老的写法：
//        Runnable runnable2 = new Runnable() {
//            @Override
//            public void run() {
//                System.out.println("执行任务1...");
//            }
//        };

//        //老的写法: 泛型代表了任务执行的返回类型
//        Callable<String> callable = new Callable<String>() {
//            @Override
//            public String call() throws Exception {
//                System.out.println("执行计算任务2...");
//                return "任务2结果";
//            }
//        };
    }
}